<template>
  <div>
    <x-card v-if="hasPerm('sysclasses:page')">
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col :md="8" :sm="24">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入班级名称"/>
            </a-col>
            <a-col  :md="8" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            </a-col>
            <a-col  :md="8" :sm="24">
              <a-button class="fr" v-if="hasPerm('sysclasses:archiveinfo')" type="primary" @click="$refs.table.refresh(true)">归档信息</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template slot="operator" v-if="hasPerm('sysclasses:add')">
          <a-button @click="$refs.addForm.add()" icon="plus" type="primary" v-if="hasPerm('sysclasses:add')">新增班级</a-button>
          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('sysclasses:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>
          <x-down
            style="display:none"
            v-if="hasPerm('sysclasses:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('sysclasses:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('sysclasses:edit') & hasPerm('sysclasses:delete')"/>
          <a-popconfirm v-if="hasPerm('sysclasses:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('sysclasses:roster') & hasPerm('sysclasses:delete')"/>
          <a v-if="hasPerm('sysclasses:roster')" @click="showHuamingce(record)">花名册</a>
          <a-divider type="vertical" v-if="hasPerm('sysclasses:archive') & hasPerm('sysclasses:roster')"/>
          <a-popconfirm v-if="hasPerm('sysclasses:archive')" placement="topRight" title="确认归档？ 归档后，所有学员将为毕业，并清空班级花名册！" @confirm="() => guidang(record)">
            <a v-if="hasPerm('sysclasses:archive')">班级归档</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" :teacherList="teacherList" :nurseList="nurseList" :classroom="classroom" :gardenHouseInfo="gardenHouseInfo" @ok="handleOk" />
      <edit-form ref="editForm" :teacherList="teacherList" :nurseList="nurseList" :classroom="classroom" :gardenHouseInfo="gardenHouseInfo" @ok="handleOk" />
    </a-card>
  </div>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
import { gardenHouseList } from '@/api/modular/garden/posManage'
import { sysPosPage, sysPosDelete, sysPosExport, getOwnUser, sysPosArchive } from '@/api/modular/garden/classManage'
import { getGardenHouse} from '@/api/modular/garden/gardenInfo';
import addForm from './addForm'
import editForm from './editForm'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm
  },

  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '班级名称',
          dataIndex: 'name'
        },
        {
          title: '开班时间',
          dataIndex: 'openTime'
        },
        {
          title: '教师',
          dataIndex: 'teacherName'
        },
        {
          title: '保育员',
          dataIndex: 'nurseName'
        },
        {
          title: '班级人数',
          dataIndex: 'childCount'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return sysPosPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      nurseList: [],
      teacherList: [],
      classroom: [],
      gardenHouseInfo: {},
    }
  },

  created () {
    if (this.hasPerm('sysclasses:edit') || this.hasPerm('sysclasses:delete')) {
      this.columns.push({
        title: '操作',
        width: '240px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    this.getUsers();
    this.getgardenHouseList();
    this.getInfos();
  },
  
  methods: {
    
        getInfos(){
            getGardenHouse().then((res) => {
                if(res.code === 200){
                    this.gardenHouseInfo = res.data;
                }
            })
        },

    //获取教师，保育员
    getUsers(){
      //sysPosEdit
      getOwnUser().then((res) => {
          if(res.code === 200){
            this.teacherList = res.data;
            this.nurseList = res.data;
          }
      })
    },
    //获取教室
    getgardenHouseList(){
      //sysPosEdit
      gardenHouseList().then((res) => {
          if(res.code === 200){
            this.classroom = res.data;
          }
      })
    },

    /**
     * 单个删除
     */
    singleDelete (record) {
      const param = [{ 'id': record.id }]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete () {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { 'id': d }
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete (param) {
      sysPosDelete(param).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    guidang (record) {
      let postData = [{
        id: record.id
      }];
      sysPosArchive(postData).then((res) => {
          if(res.code === 200){
            this.$refs.table.clearRefreshSelected()
          }
      })
    },
    showHuamingce () {

    },
    /**
     * 批量导出
     */
    batchExport () {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { 'id': d }
      })
      sysPosExport(paramIds).then((res) => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }

}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
